#ifndef _CROSSENGINE_SERVER_H_
#define _CROSSENGINE_SERVER_H_
#include "container.h"
#include "network.h"
#include "thread.h"

class NetPacket;
class IMsgHandler;

class Server:public Connection
{
public:
    explicit Server(CrossSock s);
    void SendNetPacket(NetPacket& pack);
    virtual void OnRecvNetPacket(const NetPacket& recvPack)=0;
};

struct ServerInfo
{
    ServerInfo();
    ~ServerInfo();

    Server* _server;
    Cross::AtomicNumber _state;
    CrossSock _rememberSock;
    std::string _dotip;
    uint16 _port;
    Server* (*_creator)(CrossSock,int);
};

class ServerManager:public Cross::Thread
{
public:
    virtual IMsgHandler* CreateMsgHandler();
    virtual bool Initialize(){_reconnectSeconds=10;return true;}
    void Start(bool waitConnect);
    Server* CreateServer(int index,CrossSock s);
    Server* GetServer(int index);
    void InitAddServer(const std::string& dotip,uint16 port,Server* (*creator)(CrossSock,int));
    void OnOpen(CrossSock s);
    void OnClosed(CrossSock s);
    void OnReceived(CrossSock s,NetMessage* msg);
private:
    virtual void run();
    STLVector<ServerInfo>::type _srvVec;
    int _reconnectSeconds;
};

#endif//_CROSSENGINE_SERVER_H_
